<template>
  <div class="article">
    <header>
      <nav @click="moveSlider">
        <span ref="recommend" :class="{ 'active': params.sorter === 'weight_desc' }"  @click="getInterviewList_recom">推荐</span>
        <span ref="latest" :class="{ 'active': params.sorter === '' }"  @click="getInterviewList_latest">最新</span>
      </nav>
      <div class="logo" @click="scrollToTop">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 底部滑块 -->
      <div class="slider" :style="sliderStyle"></div>
    </header>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        offset="10"
        :immediate-check="false"
        @load="onLoad"
      >
        <InterViewCard v-for="item in list" :key="item.id" :item="item"></InterViewCard>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getInterviewListApi } from '@/api/interview';
import InterViewCard from '@/components/InterViewCard.vue';
export default {
  name: 'TtArticle',
  components: {
    InterViewCard
  },
  data() {
    return {
      sliderStyle: {
        left: '0px', // 初始位置
      },
      list: [],
      loading: false,
      finished: false,
      params: {
        current: 1,
        pageSize: 10,
        sorter: 'weight_desc'
      },
      refreshing: false
    };
  },
  methods: {
    moveSlider(event) {
      const target = event.target;

      if (target.tagName === 'SPAN') {
        const offset = target.offsetLeft + (target.offsetWidth / 2) - 7; // 14px / 2
        this.sliderStyle.left = `${offset}px`;
      }
    },
    scrollToTop() {
      window.scrollTo({ top: 0, behavior: 'smooth' }); // 平滑滚动到顶部
    },
    // 上拉加载
    async onLoad() {
      this.params.current++
      await this.getInterviewList()
      this.loading = false
    },
    // 下拉刷新
    async onRefresh() {
      // 清空列表数据
      this.finished = false

      this.params.current = 1
      this.list = []
      await this.getInterviewList()
      this.refreshing = false
    },
    // 获取面经列表
    async getInterviewList() {
      const res = await getInterviewListApi(this.params)
      // console.log(res)
      this.list = this.params.current === 1 ? res.data.rows : [...this.list, ...res.data.rows]
      this.finished = this.params.current < res.data.pageTotal ? false : true
    },
    // 点击“推荐”获取推荐面经列表
    getInterviewList_recom() {
      this.params.sorter = 'weight_desc'
      this.params.current = 1
      this.list = []
      this.scrollToTop() // 滚动到顶部
      this.getInterviewList()
    },
    // 点击“最新”获取最新面经列表
    getInterviewList_latest() {
      this.params.sorter = ''
      this.params.current = 1
      this.list = []
      this.scrollToTop() // 滚动到顶部
      this.getInterviewList(true)
    },
    // 跳转到详情页
    goDetail(id) {
      console.log(id)
      // this.$router.push({ name: 'Detail', params: { id } })
    }
  },
  created() {
    this.getInterviewList()
  },
  mounted() {
    // 初始化滑块位置
    this.moveSlider({ target: this.$refs.recommend }); // 默认滑块位置在“推荐”下
  },
}
</script>

<style lang="less" scoped>
  * {
    margin: 0;
    padding: 0;
  }

  header {
    position: sticky;
    left: 0;
    top: 0;
    z-index: 999;
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px 0 18px;
    background-color: #fff;
    border-bottom: 2px solid #fbfbfb;

    nav {
      flex: 1;

      span {
        padding: 0 8px;
        font-size: 14px;
        color: #333;
        cursor: pointer; /* 添加光标样式 */
      }
    }

    .logo {
      img {
        width: 60px;
      }
    }

    .slider {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 14px;
      height: 2px;
      background-color: #222222;
      transition: all 0.3s; /* 添加过渡效果 */
    }
  }

  .active {
    font-weight: 600; /* 加粗效果 */
  }
  
  .van-list {
    padding-bottom: 50px;
  }
</style>